//
//  ExamCateListCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/1.
//

#import "ExamCateListCell.h"

@interface ExamCateListCell()

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *scoreLabel;
@property(nonatomic,strong)UIView *coinBgView;
@property(nonatomic,strong)UIImageView *coinImageView;
@property(nonatomic,strong)UILabel *coinLabel;

@end

@implementation ExamCateListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    self.backgroundColor = KbgColor;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.bgView = [[UIView alloc]init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 10;
    self.bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(@56);
        make.top.mas_equalTo(@0);
    }];
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = JPUIFontInterMedium(17);
    self.titleLabel.textColor = KFontBlackColor;
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@12);
        make.right.mas_equalTo(@-12);
        make.height.mas_equalTo(@24);
        make.centerY.mas_equalTo(self_weak_.bgView);
    }];
    
    self.scoreLabel = [[UILabel alloc]init];
    self.scoreLabel.backgroundColor = UIColorHex_(0xFF781D,0.1);
    self.scoreLabel.font = JPUIFontInterMedium(16);
    self.scoreLabel.textColor = UIColorHex(0xFF781D);
    self.scoreLabel.textAlignment = NSTextAlignmentCenter;
    self.scoreLabel.layer.cornerRadius  = 27/2.0;
    self.scoreLabel.layer.masksToBounds = YES;
    [self.bgView addSubview:self.scoreLabel];
    CGFloat s_w = [self getStringWidthWithText:self.scoreLabel.text font:self.scoreLabel.font viewHeight:27];
    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-12);
        make.width.mas_equalTo(s_w+20);
        make.height.mas_equalTo(@27);
        make.centerY.mas_equalTo(self_weak_.bgView);
    }];
    self.scoreLabel.hidden = YES;
    
    self.coinBgView = [[UIView alloc]init];
    self.coinBgView.backgroundColor = UIColorHex(0xE8FAF5);
    self.coinBgView.layer.cornerRadius = 25/2.0;
    self.coinBgView.layer.masksToBounds = YES;
    [self.bgView addSubview:self.coinBgView];
    [self.coinBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-12);
        make.width.mas_equalTo(@55);
        make.height.mas_equalTo(@25);
        make.centerY.mas_equalTo(self_weak_.bgView);
    }];
    self.coinImageView = [[UIImageView alloc]init];
    self.coinImageView.image = [UIImage imageNamed:@"mine_coin_checkin"];
    [self.coinBgView addSubview:self.coinImageView];
    [self.coinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@16);
        make.left.mas_equalTo(@10);
        make.centerY.mas_equalTo(self_weak_.coinBgView);
    }];
    self.coinLabel = [[UILabel alloc]init];
    self.coinLabel.font = JPUIFontInterMedium(16);
    self.coinLabel.textColor = KThemeColor;
    self.coinLabel.textAlignment = NSTextAlignmentRight;
    [self.coinBgView addSubview:self.coinLabel];
    [self.coinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-10);
        make.height.mas_equalTo(@19);
        make.centerY.mas_equalTo(self_weak_.coinBgView);
    }];
    
    self.coinBgView.hidden = YES;
}


-(void)setModel:(ExamPaperYearListModel *)model{
    _model = model;
    self.titleLabel.text = model.name;
    if(model.isLock&&[model.isLock isEqualToString:@"1"]){
        self.coinBgView.hidden = YES;
        if(model.isFinish&&[model.isFinish isEqualToString:@"1"]){
            self.scoreLabel.hidden = NO;
            self.scoreLabel.text = [NSString stringWithFormat:@"%ld分",model.testScore?model.testScore.integerValue:0];
            CGFloat s_w = [self getStringWidthWithText:self.scoreLabel.text font:self.scoreLabel.font viewHeight:27];
            [self.scoreLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(s_w+20);
            }];
            [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-12-s_w-20-10);
            }];
        }else{
            self.scoreLabel.hidden = YES;
            [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(@-12);
            }];
        }
    }else{
        self.coinBgView.hidden = NO;
        self.scoreLabel.hidden = YES;
        self.coinLabel.text = [NSString stringWithFormat:@"-%@",model.goldNumber];
        CGFloat c_w = [self getStringWidthWithText:self.coinLabel.text font:self.coinLabel.font viewHeight:19];
        [self.coinBgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(c_w+37);
        }];
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-12-c_w-37-10);
        }];
    }
}

@end
